package com.wzdigit.wms.wms.controller.outbound;


import com.wzdigit.framework.exception.RestStatusException;
import com.wzdigit.wms.basic.client.request.picktask.cancel.CancelPicktaskRequest;
import com.wzdigit.wms.basic.client.response.Result;
import com.wzdigit.wms.basic.client.response.picktask.cancel.PicktaskCancelResp;
import com.wzdigit.wms.wms.constant.WmsWareConstant;
import com.wzdigit.wms.wms.service.outbound.PicktaskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 * 拣货_PDA 前端控制器
 * </p>
 *
 * @author yangwenbiao
 * @since 2021-02-26
 */

@Slf4j
@RestController
@RequestMapping(value = WmsWareConstant.API_VERSION)
@Api(tags = "取消拣货_PDA")
public class CancelPickController {

    public static final String REQUEST_MODEL = "/cancel_pick_pda";

    @Autowired
    PicktaskService picktaskService;

    /**
     * 扫描待【取消拣货】的SN
     *
     * @param sn
     * @param scanMode
     * @param servletRequest
     * @param servletResponse
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "扫描SN")
    @GetMapping(value = REQUEST_MODEL + "/scan_sn")
    public Result<List<PicktaskCancelResp>> scanSn(@RequestParam @ApiParam("SN") String sn,
                                                   @RequestParam @ApiParam("拣货类型——0:单台，1:整板") Integer scanMode,
                                                   HttpServletRequest servletRequest,
                                                   HttpServletResponse servletResponse) throws Exception {
        try {
            Result<List<PicktaskCancelResp>> result = picktaskService.scanCancelPickSn(sn, scanMode, servletRequest, servletResponse);
            return result;
        } catch (RestStatusException ex) {
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw ex;
        }
    }


    /**
     * 取消拣货
     *
     * @param cancelPicktaskRequest
     * @return
     */
    @ApiOperation(value = "取消拣货——提交")
    @PostMapping(value = REQUEST_MODEL + "/commit")
    public Result commit(@RequestBody @ApiParam("待取消拣货的SN集合") CancelPicktaskRequest cancelPicktaskRequest,
                         HttpServletRequest servletRequest,
                         HttpServletResponse servletResponse) throws Exception {
        try {
            Result result = picktaskService.cancelPicktaskPda(cancelPicktaskRequest, servletRequest, servletResponse);
            return result;
        } catch (RestStatusException ex) {
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw ex;
        }
    }
}